const ci = require('miniprogram-ci');
const fs = require('fs');

(async () => {
    const privateKeyPath = '/tmp/private.key';
    fs.writeFileSync(privateKeyPath, process.env.JIANMU_PRIVATE_KEY);

    const project = new ci.Project({
        appid: process.env.JIANMU_APP_ID,
        type: process.env.JIANMU_TYPE,
        projectPath: process.env.JIANMU_PROJECT_PATH,
        privateKeyPath,
        ignores: process.env.JIANMU_IGNORES ? JSON.parse(process.env.JIANMU_IGNORES) : [],
    });

    const uploadResult = await ci.upload({
        project,
        version: process.env.JIANMU_VERSION,
        desc: process.env.JIANMU_DESC,
        settings: process.env.JIANMU_SETTINGS ? JSON.parse(process.env.JIANMU_SETTINGS) : undefined,
        onProgressUpdate: console.log,
        robot: process.env.JIANMU_ROBOT ? parseInt(process.env.JIANMU_ROBOT) : undefined,
    });

    console.log('UploadResult', uploadResult);
    process.exit(0)
})();
